/*****************************************************************************/
/*                                                                           */
/*    Domino Operation System Kernel Module                                  */
/*                                                                           */
/*    Copyright (C) 2007 Laszlo Arvai                                        */
/*                                                                           */
/*    ------------------------------------------------------------------     */
/*    krnlSystemTimer.c - System Timer (1ms) kernel module                   */
/*****************************************************************************/

///////////////////////////////////////////////////////////////////////////////
// Includes
#include <krnlSystemTimer.h>
#include "sysSettings.h"

///////////////////////////////////////////////////////////////////////////////
// Local variables

// Timer counter for heart beat message
#ifdef msgTIMER_HEART_BEAT
static dosWord l_heart_beat_time = 0;
#endif

///////////////////////////////////////////////////////////////////////////////
// Delay (ms resolution delay)
void krnlDelay(dosWord in_delay_ms)
{
	dosWord start_time = krnlGetSystemTimer();
	dosWord diff_time;
	
	do
	{
		diff_time = krnlGetSystemTimer() - start_time;
	}	while( diff_time < in_delay_ms );
}

///////////////////////////////////////////////////////////////////////////////
// Get ellapsed time since a timestamp (in ms)
dosWord krnlGetTimeSince(dosWord in_start_time_ms)
{
	dosWord diff_time;

	diff_time = krnlGetSystemTimer() - in_start_time_ms;
	
	return diff_time;
}

///////////////////////////////////////////////////////////////////////////////
// Idle handler
#ifdef msgTIMER_HEART_BEAT
void krnlSystemTimerIdleHandler(void)
{
	krnlMessage* message;

  // generate heart beat message
  if( (dosWord)(krnlGetSystemTimer() - l_heart_beat_time) > tmrHEART_BEAT_INTERVALL )
  {
    l_heart_beat_time = krnlGetSystemTimer();

    message = krnlAllocateMessage();

    if( message != DOS_NULL )
    {
      message->Id = msgTIMER_HEART_BEAT;
    }
  }
}
#endif
